package com.farmer.controller;

import com.farmer.pojo.Admin;
import com.farmer.pojo.User;
import com.farmer.service.AdminService;
import com.farmer.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("admin")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("add")
    public Result add(@RequestBody Admin admin) {
        Result result =adminService.add(admin);
        return result;
    }

    @DeleteMapping("deleteAdmin")
    public Result deleteAdmin(@RequestParam Integer id) {
        Result result = adminService.deleteAdmin(id);
        return result;
    }
  @PostMapping("deblock")
  public Result deblock(@RequestParam Integer id){
        Result result = adminService.deblock(id);
        return result;
  }
    @PutMapping("update")
    public Result update(@RequestBody Admin admin) {
        Result result = adminService.update(admin);
        return result;
    }

    @GetMapping("getById")
    public Result getById(@RequestParam Integer id) {
        Result result = adminService.getById(id);
        return result;
    }

    @GetMapping("getAll")
    public Result getAll() {
        Result result = adminService.getAll();
        return result;
    }

    /*
     * 根据标题模糊查询*/
    @GetMapping("getByTitleLike")
    public Result getByTitleLike(@RequestParam String name){
        Result result = adminService.getByTitleLike(name);
        return result;
    }

    @PostMapping("login")
    public Result login(@RequestBody Admin admin){
        Result result = adminService.login(admin);
        return result;
    }

    @GetMapping("getAdminInfo")
    public Result getAdminInfo(@RequestHeader String token){
        Result result = adminService.getAdminInfo(token);
        return result;
    }


    @PostMapping("checkUsername")
    public Result checkUsername(@RequestParam String username){
        Result result= adminService.checkUserName(username);
        return result;
    }
    @PostMapping("register")
    public  Result  register(@RequestBody Admin admin){
        Result result = adminService.register(admin);
        return result;

    }
    @PutMapping("updatePassword")
    public Result updatePassword(@RequestHeader String token,@RequestParam("newPassword") String newPassword)
    {
        Result result=adminService.updatePassword(token,newPassword);
        return result;
    }
    @PostMapping("avatar/insert")
    public Result avatarInsert(@RequestHeader("token") String token,@RequestParam("file") MultipartFile file)  {
        Result result = adminService.avatarInsert(token,file);
        return result;
    }

    @GetMapping("getAvatar")
    public Result getAvatar(@RequestParam Integer id){
        Result result=adminService.getAvatar(id);
        return result;
   }
    @GetMapping("page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        Result result=adminService.page(pageNum,pageSize);
        return result;
    }

}
